package com.ts.content.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ts.common.core.exception.RenException;
import com.ts.common.core.utils.Result;
import com.ts.common.security.dto.UserLoginDto;
import com.ts.content.dto.TopicDTO;
import com.ts.content.entity.TopicEntity;
import com.ts.content.service.TopicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/topic")
@Slf4j
public class TopicController {

    @Autowired
    private TopicService topicService;

    @PostMapping("/page")
    public IPage<TopicDTO> page(@RequestBody Page<TopicDTO> page) {
        return topicService.getTopicList(page);
    }

    @PostMapping("/save")
    public Result save(@RequestBody TopicEntity entity) {
        log.debug("topicEntity:{}", entity);
        entity.setCreateTime(new Date());
        boolean save = topicService.save(entity);
        return new Result().viewOk(entity.getId());
    }

    @GetMapping("/info/{id}")
    public List<TopicEntity> getTopicInfo(@PathVariable("id") Integer userId){
        QueryWrapper<TopicEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return topicService.list(queryWrapper);
    }

    @GetMapping("/currentUserJoinTopic")
    public Result getCurrentUserTopic( Page<TopicEntity> page,UserLoginDto userLoginDto){
        if (ObjectUtils.isEmpty(userLoginDto) || ObjectUtils.isEmpty(userLoginDto.getUserId())){
            throw new RenException("请先登录");
        }

        IPage<TopicEntity> topicEntityIPage = topicService.getCurrentUserTopicById(userLoginDto.getUserId(), page);
//        IPage<TopicEntity> topicEntityIPage = topicService.getCurrentUserTopicById(6, page);
        return new Result().viewOk(topicEntityIPage);
    }
}
